Skip to content

fix: improve performance of ApplyPowerTableDiffs in ImportSnapshotToDatastore #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Aug 1, 2025

The sort operation in ApplyPowerTableDiffs turns out to be expensive.
This PR introduces another func ApplyPowerTableDiffsToMap(powerTableMap map[gpbft.ActorID]gpbft.PowerEntry, diffs ...PowerTableDiff) (map[gpbft.ActorID]gpbft.PowerEntry, error) to avoid sorting.

It also adds more validations against first instance, latest instance and powertable cid.

Perf stats of importing a mainnet F3 snapshot on my laptop show ~99% perf gain

# PR
0.884s
0.773s
0.864s

# main
86.547s
88.422s
86.998s

@github-project-automation github-project-automation bot moved this to Todo in F3 Aug 1, 2025
Copy link

codecov bot commented Aug 1, 2025

Codecov Report

❌ Patch coverage is 53.96825% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.46%. Comparing base (9288fba) to head (c04cb35).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
certstore/snapshot.go 30.95% 18 Missing and 11 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1047      +/-   ##
==========================================
- Coverage   64.77%   64.46%   -0.31%     
==========================================
  Files          81       81              
  Lines        9896     9942      +46     
==========================================
- Hits         6410     6409       -1     
- Misses       2961     2999      +38     
- Partials      525      534       +9     
Files with missing lines Coverage Δ
certs/certs.go 94.28% <100.00%> (+0.34%) ⬆️
certstore/snapshot.go 41.61% <30.95%> (-6.22%) ⬇️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hanabi1224 hanabi1224 marked this pull request as ready for review August 1, 2025 16:28
@hanabi1224 hanabi1224 force-pushed the speed-up-apply-power-table-diffs branch from 5fbd9b1 to af0db51 Compare August 4, 2025 09:30
@rjan90 rjan90 moved this from Todo to In review in F3 Aug 4, 2025
@rjan90 rjan90 requested a review from Kubuxu August 4, 2025 14:14
@BigLep BigLep requested a review from Copilot August 12, 2025 03:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the performance of applying power table diffs during F3 snapshot import by introducing a new map-based approach that avoids expensive sorting operations on each iteration. The optimization shows a ~99% performance improvement when importing mainnet F3 snapshots.

  • Introduced ApplyPowerTableDiffsToMap function to work with power table maps instead of sorted arrays
  • Added comprehensive validation for instance numbers, latest instance bounds, and power table CID verification
  • Refactored importSnapshotToDatastoreWithTestingPowerTableFrequency to use the new map-based approach and include additional safety checks

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
certstore/snapshot.go Refactored snapshot import logic to use map-based power table operations and added validation checks
certs/certs.go Added new map-based power table diff functions and helper functions for array-map conversion

@Kubuxu
Copy link
Contributor

Kubuxu commented Aug 12, 2025

SGTM apart from the two issues highlighted by Copilot

@Kubuxu Kubuxu enabled auto-merge (squash) August 13, 2025 13:26
@Kubuxu Kubuxu merged commit 96230de into filecoin-project:main Aug 13, 2025
16 of 18 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in F3 Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants